from src.db import dbm, dbs
from sqlalchemy.orm import scoped_session


def search_host_by_node_id(node_id):
    if node_id == '0':
        return 'localhost'
    else:
        with dbm.get(f"sys_default") as db_session:
            db_session: scoped_session
            node = db_session.query(dbs.sys_default.Node).filter(dbs.sys_default.Node.node_id == node_id).first()
            if node:
                return node.host
            else:
                return None

def search_node_by_node_id(node_id):
    if node_id == '0':
        with dbm.get(f"sys_default") as db_session:
            db_session: scoped_session
            node = db_session.query(dbs.sys_default.Node).filter(dbs.sys_default.Node.host == 'localhost').first()
            if node:
                return node
            else:
                return None
    else:
        with dbm.get(f"sys_default") as db_session:
            db_session: scoped_session
            node = db_session.query(dbs.sys_default.Node).filter(dbs.sys_default.Node.node_id == node_id).first()
            if node:
                return node
            else:
                return None
        